package org.dromara.admin.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

/**
 * 主体类型枚举：个人(PERSONAL)、个体户(SELF_EMPLOYED)、企业(ENTERPRISE)
 */
@Getter
@JsonFormat(shape = JsonFormat.Shape.OBJECT) // 序列化为对象形式（code + info）
public enum SubjectTypeEnum {

    PERSONAL("PERSONAL", "个人"),
    SELF_EMPLOYED("SELF_EMPLOYED", "个体户"),
    ENTERPRISE("ENTERPRISE", "企业");

    @EnumValue
    @JsonValue
    private final String code;
    private final String info;

    SubjectTypeEnum(String code, String info) {
        this.code = code;
        this.info = info;
    }

    /**
     * 反序列化支持：前端传 code 时自动匹配
     */
    @JsonCreator
    public static SubjectTypeEnum fromCode(String code) {
        if (code == null) return null;
        for (SubjectTypeEnum e : values()) {
            if (e.code.equalsIgnoreCase(code)) {
                return e;
            }
        }
        return null;
    }

    @JsonValue
    public Map<String, String> toJson() {
        Map<String, String> map = new HashMap<>();
        map.put("code", this.code);
        map.put("info", this.info);
        return map;
    }
}
